package de.baumann.browser.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import de.baumann.browser.R;
import de.baumann.browser.api.net.vo.MineService;
import de.baumann.browser.utils.ViewUtil;

public class MineOptionAdapter extends RecyclerView.Adapter<MineOptionAdapter.OptionHolder> {

    private Context mContext;
    private List<MineService> mineServiceList;
    private BaseQuickAdapter.OnItemClickListener onItemClickListener;

    public MineOptionAdapter() {
        this.mineServiceList = ViewUtil.getMineOptions();
    }

    public void setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public OptionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_mine_option, parent, false);
        return new OptionHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OptionHolder holder, int position) {
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(null, null, position);
            }
        });

        holder.mTvOptionName.setText(mineServiceList.get(position).getFunName());
        holder.mOptionIcon.setImageResource(mineServiceList.get(position).getFunIcon());
    }

    @Override
    public int getItemCount() {
        return mineServiceList.size();
    }

    public int getId(int position){
        return mineServiceList.get(position).getId();
    }


    class OptionHolder extends RecyclerView.ViewHolder {
        ImageView mOptionIcon;
        TextView mTvOptionName;

        OptionHolder(View itemView) {
            super(itemView);
            mOptionIcon = itemView.findViewById(R.id.itemOptionIcon);
            mTvOptionName = itemView.findViewById(R.id.itemOptionName);
        }
    }
}
